package org.itboys.mobile.service.mongo.weixin;

import org.apache.commons.collections.map.HashedMap;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.trip.SundayTripDto;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mongo.SundayTrip;
import org.itboys.mobile.entity.mysql.SundayProduct;
import org.itboys.mobile.service.mysql.web.SundayProductService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Auther: 罗尧坤
 * Date: 2018/7/19
 * Description: 集楼_浏览记录_微信服务
 */

@Service
public class SundayWeiXinTripService extends BaseMongoService<SundayTrip> {

    @Resource(name = "mobileDs")
    private MongoDataSource DataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return DataSource;
    }

    @Override
    protected Class<SundayTrip> getEntityClass() {
        return SundayTrip.class;
    }

    @Autowired
    private ResourceHolder resourceHolder;
    @Autowired
    private SundayProductService productService;


    /**
     * 查询用户预约记录
     * @param request
     * @param memberId
     * @return
     */
    public List<SundayTripDto> selectList(HttpServletRequest request,Long memberId){

        Map<String, Object> param = ParamUtil.packageMongoExactParam(request);
        param.put("isDeleted", 0);
        param.put("memberId", memberId);
        param.put("sort","ct");
        param.put("order","desc");

        List<SundayTrip> data = super.pageQuery(request,param).getData();

        if(!ListUtil.isNotNull(data)){

            return new ArrayList<>();

        }

        List<SundayTripDto> result=new ArrayList<>();
        Map<String,Object> _param=new HashedMap();

        //判断用户id是否为空
        if(LongUtil.isNotZreo(memberId)){
            //传入楼盘id

            List<Long> productIds=new ArrayList<>();
            for (SundayTrip trip:data){
                productIds.add(trip.getProductId());
            }
            /**
             * 获取所有的楼盘

             * 先查出所有楼盘list
             * 在将楼盘id和浏览记录中的楼盘id对比
             * 相等就传入
             */
            _param.put("ids",productIds);
            List<SundayProduct> products = productService.select(_param);
            for (SundayTrip trip:data) {
                for (SundayProduct product:products) {
                    if (trip.getProductId().equals(product.getId())){
                        SundayTripDto tripDto = packageDto(trip, product);
                        result.add(tripDto);
                        break;
                    }
                }
            }
        }
        return result;
    }

    /**
     * 保存用户浏览记录
     * @param memberId
     * @param productId
     */
    public void save(Long memberId, Long productId){
        SundayTrip trip = new SundayTrip(memberId,productId);
        Map<String, Object> param = new HashMap<>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("memberId",memberId);
        param.put("productId",productId);
        List<SundayTrip> trips = super.list(param);
        for (SundayTrip temp :trips) {
            super.delete(temp.getId());
        }
        super.save(trip);
    }

    /**
     * 封装Dto
     * @param trip
     * @param product
     * @return
     */
    public SundayTripDto packageDto(SundayTrip trip, SundayProduct product){
        SundayTripDto result=new SundayTripDto();
        result.setId(trip.getId());
        result.setMemberId(trip.getMemberId());
        result.setLocation(product.getCategoryName());
        result.setPrice(product.getTotalPrice());//改为TotalPrice
        result.setImage(product.getDefaultImage());
        result.setEvaluateTime(product.getEvaluateTime());
        result.setProductId(trip.getProductId());
        result.setProductName(product.getName());

        return  result;
    }


}
